<?php
namespace app\admin\controller;

use app\common\model\JingjianService as JingjianServiceModel;
use app\common\model\AdminUser as AdminUserModel;

/**
 * 经书控制器
 * Class Jingjianservice
 * @package app\index\controller
 */
class Jingjianservice extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new JingjianServiceModel;
        $amodel = new AdminUserModel;
        $jingjian = new \JingjianService\JingjianService();
        $this->getPageAndSize($param);
        $map = [];
        try{
            if(!empty($param['status'])){
                $map['status'] = $param['status'];
            }else{
                $map['status'] = ['neq',2];
            }
            if(!empty($param['con'])){
                $map['sub_mchid|merchant_name|applyment_id'] = ['like', "%" . $param['con'] . "%"];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['addtime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size);
            foreach($lists as &$v){
                if($v['status'] == 1){
                    $result = $jingjian->query($v['applyment_id']);
                    if($result['applyment_state'] == 'APPLYMENT_STATE_AUDITING'){
                        $status = 3; //审核中
                    }elseif($result['applyment_state'] == 'APPLYMENT_STATE_SIGNING'){
                        $status = 4; //开通权限中
                    }elseif($result['applyment_state'] == 'APPLYMENT_STATE_FINISHED'){
                        $status = 5; //已完成
                        $v['sub_mchid'] = $result['sub_mchid'];
                        $model->editData(['id'=>$v['id'],'sub_mchid'=>$v['sub_mchid']]);
                    }elseif($result['applyment_state'] == 'APPLYMENT_STATE_REJECTED'){
                        $status = 6; //已驳回
                        $v['audit_detail'] = $result['audit_detail'][0]['reject_reason'];
                        $model->editData(['id'=>$v['id'],'audit_detail'=>$v['audit_detail']]);
                    }elseif($result['applyment_state'] == 'APPLYMENT_STATE_EDITTING'){
                        $status = 7; //编辑中
                    }elseif($result['applyment_state'] == 'APPLYMENT_STATE_TO_BE_CONFIRMED'){
                        $status = 8; //待账户验证
                    }elseif($result['applyment_state'] == 'APPLYMENT_STATE_TO_BE_SIGNED'){
                        $status = 9; //待签约
                    }elseif($result['applyment_state'] == 'APPLYMENT_STATE_CANCELED'){
                        $status = 10; //已作废
                    }
                    $model->editData(['id'=>$v['id'],'status'=>$status]);
                    $v['status'] = $status;
                }
                $admin_user = $amodel->findData($v['admin_id'],'id','username');
                $v['admin'] = $admin_user['username'];
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            }
        }catch (\Exception $e) {
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [add  添加]
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new JingjianServiceModel;
        $user = $this->user;
        try{
            if(!empty($param['addtime'])){
                $param['addtime'] = strtotime($param['addtime']);
            }
//            if(!empty($param['subject_info'])){
//                $subject = json_decode($param['subject_info']);
//                $param['merchant_name'] = $subject->certificate_info->merchant_name;
//            }
            $param['admin_id'] = $user['id'];
            $model->addData($param);
        }catch (\Exception $e){
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'添加成功');
    }
    /**
     * [edit  编辑]
     * @return json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $model = new JingjianServiceModel;
        try {
            if(!empty($param['addtime'])){
                $param['addtime'] = strtotime($param['addtime']);
            }
            if(!empty($param['subject_info'])){
                $subject = json_decode($param['subject_info']);
                $param['merchant_name'] = $subject->certificate_info->merchant_name;
            }
            $model->editData($param);
        }catch (\Exception $e){
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'编辑成功');
    }
    /**
     * [jingjian  进件接口]
     * @return json
     * @author [C。]
     */
    public function jingjian()
    {
        $param = input('param.');
        $model = new JingjianServiceModel();
        try{
            $data = $model->findData($param['id'],'id','business_code,contact_info,subject_info,business_info,settlement_info,bank_account_info,addition_info');
            $data['contact_info'] = json_decode($data['contact_info']);
            $data['subject_info'] = json_decode($data['subject_info']);
            $data['business_info'] = json_decode($data['business_info']);
            $data['settlement_info'] = json_decode($data['settlement_info']);
            $data['bank_account_info'] = json_decode($data['bank_account_info']);
            $data['addition_info'] = json_decode($data['addition_info']);
            $jingjian = new \JingjianService\JingjianService();
            $result = $jingjian->submit($data);
            if($result['code'] == 1){
                $model->editData(['id'=>$param['id'],'applyment_id'=>$result['msg'],'status'=>1]);
            }else{
                return admin_json(-1,'进件失败',$result['msg']);
            }
        }catch (\Exception $e){
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'进件成功');
    }
    /**
     * [upload_img  上传图片]
     * @return json
     * @author [C。]
     */
    public function upload_img()
    {
        if (!empty($_FILES['file']['name'])) {
            if($_FILES['file']['size'] > 10*1024*1024){
                return admin_json(-2,'图片不能超过10M');
            }
            $fileName = $_FILES['file']['name'];    //得到文件全名
            $dotArray = explode('.', $fileName);    //把文件名安.区分，拆分成数组
            $type = end($dotArray);
            if ($type != "png" && $type != "gif" && $type != "jpeg" && $type != "bmp" && $type != "jpg") {
                return admin_json(0, '请上传图片文件！');
            }
            //取数组最后一个元素，得到文件类型
            $uploaddir = "upload/";//设置文件保存目录 注意包含

            if (!file_exists($uploaddir)) {
                mkdir($uploaddir, 0755, true);
            }
            $path = $uploaddir . md5(uniqid(rand())) . '.' . $type; //产生随机文件名
            //$path = "images/".$fileName; //客户端上传的文件名；
            //下面必须是tmp_name 因为是从临时文件夹中移动
            move_uploaded_file($_FILES['file']['tmp_name'], $path); //从服务器临时文件拷贝到相应的文件夹下
            $image = \think\Image::open('./'.$path);
            $image->thumb(550, 550)->save('./'.$path);
            if (!file_exists($path)) {
                $ret['res'] = "0";
                $ret['msg'] = "上传文件丢失!";
                return json_encode($ret);
            }
        }else {
            return admin_json(-2,'上传文件失败');
        }
        $jingjian = new \JingjianService\Upload();
        $data = $jingjian->upload($path);
        $oss_filename = uploadoss($path,'jingjian');
        $data['filename'] = $oss_filename;
        return admin_json(1,'上传成功',$data);
    }
}